package com.cc.dao.impl;

import com.cc.dao.BaseDao;
import com.cc.dao.DeptDao;
import com.cc.entity.Dept;
import com.cc.util.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 33314
 * @version 1.0
 * @description: TODO
 * @date 2025/4/17 19:31
 */
public class DeptDaoImpl extends BaseDao implements DeptDao {
    @Override
    public int addDept(Dept dept) {
        String sql = "insert into deptment(deptName,description) values(?,?)";
        return executeUpdate(sql,dept.getDeptName(),dept.getDescription());
    }

    @Override
    public int deleteDept(int id) {
        String sql = "delete from deptment where id = ?";
        return executeUpdate(sql,id);
    }

    @Override
    public int updateDept(Dept dept) {
        String sql = "update deptment set deptName = ?,description = ? where id = ?";
        return executeUpdate(sql,dept.getDeptName(),dept.getDescription(),dept.getId());
    }

    @Override
    public List<Dept> getDept() {
        String sql = "select * from deptment";
        ResultSet rs = executeQuery(sql);
        List<Dept> deptList = new ArrayList<>();
        if (rs != null){
            try {
                while (rs.next()){
                    Dept dept = new Dept();
                    dept.setId(rs.getInt("id"));
                    dept.setDeptName(rs.getString("dep_name"));
                    dept.setDescription(rs.getString("description"));
                    deptList.add(dept);
                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                closedAll(rs);
            }
    }
        return deptList;
}
}
